module manager {

    export class TickManager {

        private static instance: TickManager;

        private tickObjs: Array<ITick>;

        private time: number = 0;

        public static getInstance(): TickManager {
            if (TickManager.instance == null) {
                TickManager.instance = new TickManager();
            }
            return TickManager.instance;
        }

        public constructor() {
            this.tickObjs = new Array<ITick>();
        }

        public addTick(tickObj: ITick): void {
            if (this.tickObjs.indexOf(tickObj) == -1) {
                this.tickObjs.push(tickObj);
            }
        }

        public removeTick(tickObj: ITick): void {
            var index = this.tickObjs.indexOf(tickObj);
            if (index != -1) {
                this.tickObjs.splice(index, 1);
            }
        }

        public tick(): void {
            var len = this.tickObjs.length;
            var time = egret.getTimer();
            if (this.time <= 0) {
                this.time = time;
                return;
            }
            var dt = time - this.time;
            this.time = time;
            for (var i: number = 0; i < len; i++) {
                this.tickObjs[i].onTick(dt);
            }
        }
    }

    export interface ITick {

        onTick(dt: number): void;

    }
}